﻿using System;
using SubForum.ObjectModel.PostStatuses;
using SubForum.ObjectModel.Users;
using SubForum.ObjectModel.Tags;
using SubForum.ObjectModel.Categories;
using SubForum.Framework.Utilities;
using System.Text;

namespace SubForum.ObjectModel.Posts
{
    public class Post
    {

        #region Properties

        /// <summary>
        /// The ID of the post
        /// </summary>
        public Guid PostId
        {
            get;
            set;
        }


        /// <summary>
        /// The Slug for the post's URL
        /// </summary>
        public string Slug
        {
            get;
            set;
        }

        /// <summary>
        /// The Type of Posting
        /// </summary>
        public PostType PostType
        {
            get;
            set;
        }
               
        /// <summary>
        /// The user who posted the post
        /// </summary>
        public UserProfile PostedBy
        {
            get;
            set;
        }

        /// <summary>
        /// The Topic for the Post
        /// </summary>
        public string TitleText
        {
            get;
            set;
        }

        /// <summary>
        /// The Post Text
        /// </summary>
        public string BodyText
        {
            get;
            set;
        }

        /// <summary>
        /// The Post Date
        /// </summary>
        public DateTime PostDate
        {
            get;
            set;
        }

        /// <summary>
        /// The number of views a post has had
        /// </summary>
        public int Views
        {
            get;
            set;
        }

        /// <summary>
        /// The number of replies to the post 
        /// </summary>
        public int Replies
        {
            get;
            set;
        }

        /// <summary>
        /// The status of the post
        /// </summary>
        public PostStatusType PostStatus
        {
            get;
            set;
        }

        /// <summary>
        /// The number of votes the post got
        /// </summary>
        public int Votes
        {
            get;
            set;
        }

        /// <summary>
        /// The Tags related to the post
        /// </summary>
        public string TagsText
        {
            get;
            set;
        }

        /// <summary>
        /// The Tags related to the post
        /// </summary>
        public TagList<Tag> Tags
        {
            get;
            set;
        }

        /// <summary>
        /// The Categories this post belongs to 
        /// </summary>
        public CategoryList<Category> Categories
        {
            get;
            set;
        }

        #endregion

        #region Constructors
        
        public Post()
        {
            Tags = new TagList<Tag>();
        }
        
        #endregion

        #region Methods

        public string GetPreview()
        {
            if (this.BodyText.Length > 200)
            {
                return this.BodyText.Substring(0, 200);
            }
            else
                return this.BodyText;
        }

        public string GetSlugURL(string actionName)
        {
            return actionName + "/" + this.Slug;
        }

        public string GetShortIdURL(string actionName)
        {
            return actionName + "/" + this.GetShortId();
        }

        public string GetShortId()
        {
            return ShortGuid.Encode(this.PostId);
        }

        public void SetIdFromShortId(string value)
        {
            if (value != ShortGuid.Encode(new Guid()))
            {
                this.PostId = ShortGuid.Decode(value);
            }
            else
            {
                this.PostId = new Guid();
            }
        }

        #endregion
    }
}
